package com.yyq.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.yyq.eduservice.entity.EduTeacher;
import com.yyq.eduservice.entity.vo.TeacherQuery;
import com.yyq.eduservice.service.EduTeacherService;
import com.yyq.result.R;
import com.yyq.servicebase.exceptionhandler.YyqException;
import com.zaxxer.hikari.util.FastList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *Spring的复杂性不是来自于它处理的对象，而是来自于自身，
 * 不断演进发展的Spring会带来时间维度上复杂性，比如SpringMVC以前版本的*@RequestMapping*，
 * 到了新版本被下面新注释替代，相当于增加的选项：
 *
 * @GetMapping
 * @PostMapping
 * @PutMapping
 * @DeleteMapping
 * @PatchMapping
 * 从命名约定我们可以看到每个注释都是为了处理各自的传入请求方法类型，
 * 即*@GetMapping用于处理请求方法的GET类型，@ PostMapping用于处理请求方法的POST*类型等。
 * 如果我们想使用传统的*@RequestMapping*注释实现URL处理程序，那么它应该是这样的：
 * @RequestMapping(value = “/get/{id}”, method = RequestMethod.GET)
 * 新方法可以简化为：
 * @GetMapping("/get/{id}")
 * @author yyq
 * @since 2021-03-11
 */
@CrossOrigin   //解决跨域问题
@Api("讲师管理")
@RestController  //@RestController注解相当于@ResponseBody ＋ @Controller合在一起的作用。
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    //访问地址：http://localhost:8001/duservice/teacher/findlist
    //注入Sercice
    @Autowired
    private EduTeacherService teacherService;

    @GetMapping("findlist")
    @ApiOperation(value = "讲师列表")
    public R list(){
//        自定义异常 1.创建异常类 @data @Allargc,@noarg 有参无参构造器 继承RUnException
//                  2.添加在那个全局异常类
//
//        try {
//            int i=10/0;
//        }
//        catch (Exception e){
//         throw    new YyqException(200001,"自定义异常");
//        }

        List<EduTeacher> list=teacherService.list(null);
        return  R.ok().data("item",list);

    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "根据ID删除讲师")
    public  R RemoveByID(
            @ApiParam(name="id",value = "讲师ID",required = true)
            @PathVariable String id){
        teacherService.removeById(id);
        return  R.ok();
    }

    @GetMapping("pagelistteacher/{current}/{size}")
    @ApiOperation(value = "讲师分页查询")
    public  R PageListTeacher(
            @ApiParam(name="current",value = "当前页",required = true)
            @PathVariable long current,
            @ApiParam(name="size",value = "页面大小",required = true)
            @PathVariable long size){
        Page<EduTeacher> teacher=new Page<>(current,size);
        teacherService.page(teacher, null);
        return  R.ok().data("total",teacher.getTotal()).data("list",teacher.getRecords());
    }


    //多项选择分页查询
    //requestbody 通过json传数据
    @ApiOperation(value = "多条件分页查询")
    @PostMapping("tiaojianpagelistteacher/{current}/{size}")
    public R PageListTeacherTJ(
            @PathVariable long current,
            @PathVariable long size,@RequestBody(required = false) TeacherQuery teacherQuery
            ){
        //TeacherQuery 封装的查询对象
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //page对象
        Page<EduTeacher> teacherPage=new Page<>(current,size);
        //查询条件
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        //注意Column 是数据库属性名
        if (name!=null) {
            wrapper.like("name",name);
        }
        if (level!=null) {
            wrapper.eq("level",level);
        }
        if (begin!=null) {
            wrapper.ge("gmt_create", begin);
        }

        if (end!=null) {
           wrapper.le("gmt_create", end);
        }
        //分页
        wrapper.orderByDesc("gmt_create");
         teacherService.page(teacherPage, wrapper);
        //返回总个数 和 条件查询得到的LISt
        return  R.ok().data("total",teacherPage.getTotal()).data("list",teacherPage.getRecords());

    }

    //添加
    @PostMapping("addteacher")
    @ApiOperation(value = "讲师添加") //不能添加id gmt_date 等 因为MYbatis plus 有主键 自动填充
    public  R addteacher(
            @ApiParam(name = "eduTeacher",value = "添加讲师信息，不能添加id gmt_date(自动填充)",required = true)
         @RequestBody   EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if(save)
            return R.ok();

        else return  R.error();
    }


    //通过ID查询
    @ApiOperation("通过ID查询教师")
    @GetMapping("findTeacherbyID/{id}")
    public   R findTeacherByID(
            @ApiParam(name="id",value = "讲师ID",required = true)
            @PathVariable String id){
        EduTeacher byId = teacherService.getById(id);
        return R.ok().data("teacher",byId);
    }

    //修改教师 注意！！传值需要ID
    @ApiOperation("修改教师信息")
    @PostMapping("modifyTeacher")
    public  R modifyTeacher(
            @ApiParam(name = "eduTeacher",value = "讲师信息(需要ID，不需要时间)",required = true)
    @RequestBody        EduTeacher eduTeacher
    ){
        boolean b = teacherService.updateById(eduTeacher);
        if(b)
        return R.ok();
        else return R.error();
    }


}



